use anyhow::Result;

use crate::db::db_init::db_init;



pub async fn db_insert(key: &str, value: &str) ->Result<()> {
    let db_pool = db_init().await;
    let mut tx = db_pool.begin().await?; // 开启事务
    sqlx::query("insert or replace into config (key, value) values (?, ?)")
        .bind(key)
        .bind(value)
        .execute(&mut *tx)
        .await?;
    tx.commit().await?; // 提交事务
    Ok(())
}

// 没有开启事务
// pub fn db_insert(key: &str, value: &str) ->Result<()> {
//     let mut conn = db_init().unwrap();
//     conn.execute("insert into config (key, value) values (?1, ?2)",
//      params![key, value])?;
//     return Ok(());
// }
